using System;
using System.Collections.Generic;
using System.Text;

namespace DocLibApp
{
    public class doc_template
    {
        /// <summary>
        /// get a list of templates
        /// </summary>
        /// <returns></returns>
        public static System.Collections.ArrayList GetListOfTemplates()
        {
            System.Collections.ArrayList alist = new System.Collections.ArrayList();

            List<string> FieldList = new List<string>();
            FieldList.Add("TEMPLATE_ID");
            FieldList.Add("DESCRIPTION");
            FieldList.Add("AUTHOR");

            System.Collections.Hashtable ht = new System.Collections.Hashtable();

            FormMain.DocLib_QB.TableName = def.tb_template_header;
            FormMain.DocLib_QB.Get_Select(FieldList, ht);
            alist = FormMain.DocLib_QB.GetDataRow_ArrayList(true);

            return alist;
        }

        public static System.Collections.Hashtable Get_Template_Header(string template_id)
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();

            System.Collections.Hashtable htwhere = new System.Collections.Hashtable();
            htwhere["TEMPLATE_ID"] = template_id;

            FormMain.DocLib_QB.TableName = def.tb_template_header;
            FormMain.DocLib_QB.Get_Select(htwhere);
            ht = FormMain.DocLib_QB.GetDataOneRowInHash();

            return ht;
        }

        public static string Save_Template(System.Collections.Hashtable htHeader, System.Collections.Hashtable htField)
        {
            string res = "";

            string template_id = htHeader["TEMPLATE_ID"].ToString();
            
            //update template
            if (template_exist(template_id))
            {
                //header
                htHeader["DT_UPDATE"] = SQLiteDB.DateTimeToString(System.DateTime.Now);

                System.Collections.Hashtable htWhere = new System.Collections.Hashtable();
                htWhere["TEMPLATE_ID"] = template_id;

                FormMain.DocLib_QB.TableName = def.tb_template_header;
                FormMain.DocLib_QB.Get_Update(htHeader, htWhere);
                FormMain.DocLib_QB.RunQuery();

                //if existing template, need to clear all the template fields
                System.Collections.Hashtable htDelete = new System.Collections.Hashtable();
                htDelete["TEMPLATE_ID"] = template_id;
                FormMain.DocLib_QB.TableName = def.tb_template_field;
                FormMain.DocLib_QB.Get_Delete(htDelete);
                FormMain.DocLib_QB.RunQuery();
            }
            else //create new template
            {
                //header
                SQLiteDB.AddDateDefaultToHash(ref htHeader);

                FormMain.DocLib_QB.TableName = def.tb_template_header;
                FormMain.DocLib_QB.Get_Insert(htHeader);
                FormMain.DocLib_QB.RunQuery();
            }

            SaveTemplateFields(template_id, htField);

            return res;
        }

        /// <summary>
        /// update or creat new template fields
        /// </summary>
        /// <param name="template_id"></param>
        /// <param name="ht"></param>
        private static void SaveTemplateFields(string template_id, System.Collections.Hashtable ht)
        {
            int count = 0;

            foreach (string FieldName in ht.Keys)
            {
                System.Collections.Hashtable htInsert = new System.Collections.Hashtable();

                int order_no = count + 1;

                string DefaultValue = ht[FieldName].ToString();

                htInsert["TEMPLATE_ID"] = template_id;
                htInsert["FIELD_NAME"] = FieldName;
                htInsert["DEFAULT_VALUE"] = DefaultValue;
                htInsert["ORDER_NO"] = order_no;

                FormMain.DocLib_QB.TableName = def.tb_template_field;
                FormMain.DocLib_QB.Get_Insert(htInsert);
                FormMain.DocLib_QB.RunQuery();

                count++;
            }
        }

        private static bool ExistingTemplateField(string template_id, string FieldName)
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht["TEMPLATE_ID"] = template_id;
            ht["FIELD_NAME"] = FieldName;

            FormMain.DocLib_QB.TableName = def.tb_template_field;
            FormMain.DocLib_QB.Get_Select(ht);
            if (FormMain.DocLib_QB.GetRecordCount() > 0)
                return true;

            return false;
        }

        /// <summary>
        /// Get field and default values for a template
        /// tested
        /// </summary>
        /// <param name="template_id"></param>
        /// <returns></returns>
        public static System.Collections.Hashtable Get_Template_Fields(string template_id)
        {
            //search for it
            System.Collections.Hashtable ht_where = new System.Collections.Hashtable();
            ht_where["TEMPLATE_ID"] = template_id;

            List<string> FieldList = new List<string>();
            FieldList.Add("FIELD_NAME");
            FieldList.Add("DEFAULT_VALUE");

            FormMain.DocLib_QB.TableName = def.tb_template_field;
            FormMain.DocLib_QB.Get_Select(FieldList, ht_where);
            string sql = FormMain.DocLib_QB.SQL + " ORDER BY [ORDER_NO]";

            //parse the result
            System.Collections.ArrayList alist = FormMain.DocLib_SQLite.GetRowsWithDataReader(sql, false, false);

            System.Collections.Hashtable htTemplateFieldInfo = new System.Collections.Hashtable();

            foreach (object obj in alist)
            {
                if (obj is List<string>)
                {
                    List<string> aFieldInfo = obj as List<string>;
                    string FieldName = aFieldInfo[0];
                    string DefaultValue = aFieldInfo[1];

                    htTemplateFieldInfo[FieldName] = DefaultValue;
                }
            }

            return htTemplateFieldInfo;
        }

        public static bool template_exist(string template_id)
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht["TEMPLATE_ID"] = template_id;

            FormMain.DocLib_QB.TableName = def.tb_template_header;
            FormMain.DocLib_QB.Get_Select(ht);

            return FormMain.DocLib_QB.IfRecordExist();
        }

    }
}
